package com.woniu.register.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.register.dto.DirectoryLiDTO;
import com.woniu.register.entity.*;
import com.woniu.register.mapper.*;
import com.woniu.register.service.DirectoryService;
import com.woniu.register.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zhulongxing
 * @date ：Created in 2022/12/6 19:20
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class DirectoryServiceImpl implements DirectoryService {
    @Autowired
    DirectoryMapper directoryMapper;
    @Autowired
    PledgelogoutMapper pledgelogoutMapper;
    @Autowired
    PledgeMapper pledgeMapper;
    @Autowired
    CheckMapper checkMapper;
    @Autowired
    NoticeLoginMapper noticeLoginMapper;
    @Autowired
    NoticeTypeMapper noticeTypeMapper;
    @Autowired
    NoticeLogoutMapper noticeLogoutMapper;
    @Autowired
    SealMapper sealMapper;
    @Autowired
    SeallogoutMapper seallogoutMapper;
    @Autowired
    DemurLoginMapper demurLoginMapper;
    @Autowired
    DemurLogoutMapper demurLogoutMapper;

    @Override
    public YydjVO selectYydjByDirectoryId(Long directoryId) {
        List<DemurLogin> demurLogins = demurLoginMapper.selectByDirectoryId(directoryId);
        List<DemurLogout> demurLogouts = demurLogoutMapper.selectByDirectoryId(directoryId);
        DemurLogin demurLogin = new DemurLogin();
        DemurLogout demurLogout = new DemurLogout();
        YydjVO yydjVO = new YydjVO();
        SyqVO1 syqVO1 = directoryMapper.selectSyq1VOByDirectoryId(directoryId);
        yydjVO.setRoomNo(syqVO1.getRoomNo());
        yydjVO.setProposerName(syqVO1.getProposerName());
        if (demurLogins.size()>0){
            demurLogin = demurLogins.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) demurLogin.getCheckId());
            yydjVO.setAa1(check.getAssignee() + "/" + check.getApproval());
            yydjVO.setYyZh(demurLogin.getBusinessNum());
            yydjVO.setDemurLoginMatters(demurLogin.getDemurLoginMatters());
            yydjVO.setYyTime(demurLogin.getRegisterTime());
            yydjVO.setExcursus(demurLogin.getExcursus());
        }
        if (demurLogouts.size()>0){
            demurLogout = demurLogouts.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) demurLogout.getCheckId());
            yydjVO.setAa2(check.getAssignee() + "/" + check.getApproval());
            yydjVO.setYyzxZh(demurLogout.getBusinessNum());
            yydjVO.setYyzxTime(demurLogout.getRegisterTime());
        }
        return yydjVO;
    }

    @Override
    public CfdjVO selectCfdjByDirectoryId(Long directoryId) {
        List<Seal> seals = sealMapper.selectByDirectoryId(directoryId);
        List<Seallogout> seallogouts = seallogoutMapper.selectByDirectoryId(directoryId);
        Seal seal = new Seal();
        Seallogout seallogout = new Seallogout();
        CfdjVO cfdjVO = new CfdjVO();
        SyqVO1 syqVO1 = directoryMapper.selectSyq1VOByDirectoryId(directoryId);
        cfdjVO.setRoomNo(syqVO1.getRoomNo());
        if(seals.size()>0){
            seal = seals.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) seal.getCheckId());
            cfdjVO.setAa1(check.getAssignee() + "/" + check.getApproval());
            cfdjVO.setCfZh(seal.getBusinessNum());
            cfdjVO.setSealofficeName(seal.getSealofficeName());
            cfdjVO.setSealReference(seal.getSealReference());
            cfdjVO.setSealDate(seal.getSealDate());
            cfdjVO.setSealDeadline(seal.getSealDeadline());
            cfdjVO.setCfDate(seal.getRegisterTime());
            cfdjVO.setExcursus(seal.getExcursus());
        }
        if (seallogouts.size()>0) {
            seallogout = seallogouts.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) seallogout.getCheckId());
            cfdjVO.setAa2(check.getAssignee() + "/" + check.getApproval());
            cfdjVO.setCfzxZh(seallogout.getBusinessNum());
            cfdjVO.setReleaseReference(seallogout.getReleaseReference());
            cfdjVO.setReleaseDate(seallogout.getReleaseDate());
            cfdjVO.setCfzxDate(seallogout.getRegisterTime());
        }
        return cfdjVO;
    }

    /**
     * 返回登记薄预告登记页面信息
     * @param directoryId
     * @return
     */
    @Override
    public YgdjVO selectYgdjByDirectoryId(Long directoryId) {
        List<NoticeLogin> noticeLogins = noticeLoginMapper.selectByDirectoryId(directoryId);
        List<NoticeLogout> noticeLogouts = noticeLogoutMapper.selectByDirectoryId(directoryId);
        NoticeLogin noticeLogin = new NoticeLogin();
        NoticeLogout noticeLogout = new NoticeLogout();
        YgdjVO ygdjVO = new YgdjVO();
        SyqVO1 syqVO1 = directoryMapper.selectSyq1VOByDirectoryId(directoryId);
        ygdjVO.setRoomNo(syqVO1.getRoomNo());
        ygdjVO.setProposerName(syqVO1.getProposerName());
        ygdjVO.setProposerCard(syqVO1.getProposerCard());
        if(noticeLogins.size()>0){
            noticeLogin = noticeLogins.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) noticeLogin.getCheckId());
            ygdjVO.setYgZh(noticeLogin.getBusinessNum());
            NoticeType noticeType = noticeTypeMapper.selectByPrimaryKey((long) noticeLogin.getNoticeTypeId());
            ygdjVO.setNoticeTypeName(noticeType.getNoticeTypeName());
            ygdjVO.setNoticeLoginOn(noticeLogin.getNoticeLoginOn());
            ygdjVO.setYgTime(noticeLogin.getRegisterTime());
            ygdjVO.setAa1(check.getAssignee() + "/" + check.getApproval());
            ygdjVO.setExcursus(noticeLogin.getExcursus());
        }
        if (noticeLogouts.size()>0){
            noticeLogout =  noticeLogouts.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) noticeLogout.getCheckId());
            ygdjVO.setAa2(check.getAssignee() + "/" + check.getApproval());
            ygdjVO.setYgzxZh(noticeLogout.getBusinessNum());
            ygdjVO.setYgzxTime(noticeLogout.getRegisterTime());
        }
        return ygdjVO;
    }

    /**
     * 返回登记薄现房抵押页面信息
     * @param directoryId
     * @return
     */
    @Override
    public XfdyVO selectXfdyByDirectoryId(Long directoryId) {
        List<Pledgelogout> pledgelogouts = pledgelogoutMapper.selectByDirectoryId(directoryId);
        List<Pledge> pledges = pledgeMapper.selectByDirectoryId(directoryId);
        Pledge pledge = new Pledge();
        Pledgelogout pledgelogout = new Pledgelogout();
        XfdyVO xfdyVO = new XfdyVO();
        JbzkVO jbzkVO = directoryMapper.selectJbzkByDirectoryId(directoryId);
        xfdyVO.setRoomNo(jbzkVO.getRoomNo());
        if(pledges.size()>0) {
            pledge = pledges.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) pledge.getCheckId());
//        xfdyVO.setDirectoryId(Integer.parseInt(directoryId.toString()));
            xfdyVO.setAa1(check.getAssignee() + "/" + check.getApproval());
            xfdyVO.setDyZh(pledge.getBusinessNum());
            xfdyVO.setBak1(pledge.getBak1());
            xfdyVO.setPledgerName(pledge.getPledgerName());
            xfdyVO.setPledgeeName(pledge.getPledgeeName());
            xfdyVO.setSecuredAmount(pledge.getSecuredAmount());
            xfdyVO.setDebtDate(pledge.getDebtDate());
            xfdyVO.setDyTime(pledge.getRegisterTime());
            xfdyVO.setExcursus(pledge.getExcursus());
        }
        if(pledgelogouts.size()>0){
            pledgelogout = pledgelogouts.get(0);
            Check check1 = checkMapper.selectByPrimaryKey((long) pledgelogout.getCheckId());
            xfdyVO.setAa2(check1.getAssignee() + "/" + check1.getApproval());
            xfdyVO.setZxZh(pledgelogout.getBusinessNum());
            xfdyVO.setZxTime(pledgelogout.getRegisterTime());
        }
        return xfdyVO;
    }

    /**
     * 返回登记薄在建抵押页面信息
     * @param directoryId
     * @return
     */
    @Override
    public ZjdyVO selectZjdyByDirectoryId(Long directoryId) {
        List<Pledgelogout> pledgelogouts = pledgelogoutMapper.selectByDirectoryId(directoryId);
        List<Pledge> pledges = pledgeMapper.selectZjdyByDirectoryId(directoryId);
        Pledge pledge = new Pledge();
        Pledgelogout pledgelogout = new Pledgelogout();
        ZjdyVO zjdyVO = new ZjdyVO();
        JbzkVO jbzkVO = directoryMapper.selectJbzkByDirectoryId(directoryId);
        zjdyVO.setRoomNo(jbzkVO.getRoomNo());
        zjdyVO.setHigherRoomRepose(jbzkVO.getHigherRoomRepose());
        if(pledges.size()>0) {
            pledge = pledges.get(0);
            Check check = checkMapper.selectByPrimaryKey((long) pledge.getCheckId());
//        xfdyVO.setDirectoryId(Integer.parseInt(directoryId.toString()));
            zjdyVO.setAa1(check.getAssignee() + "/" + check.getApproval());
            zjdyVO.setDyZh(pledge.getBusinessNum());
            zjdyVO.setBak1(pledge.getBak1());
            zjdyVO.setPledgerName(pledge.getPledgerName());
            zjdyVO.setPledgeeName(pledge.getPledgeeName());
            zjdyVO.setSecuredAmount(pledge.getSecuredAmount());
            zjdyVO.setDebtDate(pledge.getDebtDate());
            zjdyVO.setDyTime(pledge.getRegisterTime());
            zjdyVO.setExcursus(pledge.getExcursus());
        }
        if(pledgelogouts.size()>0){
            pledgelogout = pledgelogouts.get(0);
            Check check1 = checkMapper.selectByPrimaryKey((long) pledgelogout.getCheckId());
            zjdyVO.setAa2(check1.getAssignee() + "/" + check1.getApproval());
            zjdyVO.setZxZh(pledgelogout.getBusinessNum());
            zjdyVO.setZxTime(pledgelogout.getRegisterTime());
        }
        return zjdyVO;
    }

    /**
     * 返回登记薄所有权页面信息
     * @param directoryId
     * @return
     */
    @Override
    public Map<String, Object> selectSyqByDirectoryId(Long directoryId) {
        SyqVO1 syqVO1 = directoryMapper.selectSyq1VOByDirectoryId(directoryId);
        SyqVO2 syqVO2 = directoryMapper.selectSyq2VOByDirectoryId(directoryId);
        syqVO1.setAa(syqVO1.getAssignee()+"/"+syqVO1.getApproval());
        if (syqVO2 == null) {
            syqVO1.setGyqk("否");
            syqVO2.setBusinessNum("/");
            syqVO2.setCommonerName("/");
            syqVO2.setCommonerCard("/");
            syqVO2.setCommonerAddress("/");
            syqVO2.setGyqk("/");
            syqVO2.setHigherSrcid("/");
            syqVO2.setHigherLandNature("/");
            syqVO2.setOwnerErtificateNum("/");
            syqVO2.setRegisterTime(null);
            syqVO2.setAa("/");
        }else {
            syqVO1.setGyqk("是");
            syqVO2.setGyqk("是");
            syqVO2.setAa(syqVO2.getAssignee()+"/"+syqVO2.getApproval());
        }
        Map<String, Object> map = new HashMap();
        map.put("syqVO1",syqVO1);
        map.put("syqVO2",syqVO2);
        return map;
    }

    /**
     * 查询登记薄页面基本状况信息
     * @param directoryId
     * @return
     */
    @Override
    public JbzkVO selectJbzkByDirectoryId(Long directoryId) {
        JbzkVO jbzkVO = directoryMapper.selectJbzkByDirectoryId(directoryId);
        jbzkVO.setAa(jbzkVO.getAssignee()+"/"+jbzkVO.getApproval());
        if (jbzkVO.getCommonerName()!=null){
            jbzkVO.setProposerName(jbzkVO.getProposerName()+"(50)"+jbzkVO.getCommonerName()+"(50)");
        }else {
            jbzkVO.setProposerName(jbzkVO.getProposerName()+"(100)");
        }
        return jbzkVO;
    }

    /**
     * 查询登记薄封面信息
     * @param directoryId
     * @return
     */
    @Override
    public DirectoryLiVO selectDirectoryNumAndHigherMound(Long directoryId) {
        return directoryMapper.selectDirectoryNumAndHigherMound(directoryId);
    }

    /**
     * 登记薄首页得到DirectoryLiVO集合
     * @return
     */
    @Override
    public List<DirectoryLiVO> selectAllDirectory(DirectoryLiDTO directoryLiDTO) {
        PageHelper.startPage(directoryLiDTO.getCurrentPage(),directoryLiDTO.getPageSize());
        List<DirectoryLiVO> list = directoryMapper.selectAllDirectory(directoryLiDTO);
        for (DirectoryLiVO directoryLiVO : list) {
            directoryLiVO.setAa(directoryLiVO.getAssignee()+"/"+directoryLiVO.getApproval());
            if (directoryLiVO.getCommonerName()!=null){
                directoryLiVO.setProposerName(directoryLiVO.getProposerName()+"(50)"+directoryLiVO.getCommonerName()+"(50)");
            }else {
                directoryLiVO.setProposerName(directoryLiVO.getProposerName()+"(100)");
            }
        }
        return list;
    }

    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    public int insert(Directory record) {
        return 0;
    }

    public int insertSelective(Directory record) {
        return directoryMapper.insertSelective(record);
    }

    public Directory selectByPrimaryKey(Long id) {
        return null;
    }



    @Override
    public int updateByPrimaryKeySelective(Directory record) {
        return directoryMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Directory record) {
        return 0;
    }

    @Override
    public int updateStatue(String directoryNum) {
        Directory directory = directoryMapper.selectByDirectoryNum(directoryNum);
        directory.setDirectoryStatus("2");
        int i = directoryMapper.updateByPrimaryKeySelective(directory);
        return i;
    }

    @Override
    public int insertDirectory() {
        return directoryMapper.insertDirectory();
    }

    @Override
    public int selectId() {
        return directoryMapper.selectId();
    }
}
